package com.zjun.threadpool.proxy;


import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.lang.reflect.Proxy;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 拒绝策略代理工具类
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class RejectedProxyUtil {

    /**
     * 创建拒绝策略代理器
     * @param rejectedExecutionHandler 真正的线程池拒绝策略执行器
     * @param countNum 执行策略执行统计器
     * @return 代理拒绝策略
     */
    public static RejectedExecutionHandler createProxy(RejectedExecutionHandler rejectedExecutionHandler,
                                                       AtomicLong countNum){
        return (RejectedExecutionHandler) Proxy.newProxyInstance(rejectedExecutionHandler.getClass().getClassLoader(),
                new Class[]{rejectedExecutionHandler.getClass()},
                new RejectedProxyInvacationhandler(rejectedExecutionHandler,countNum));
    }

}
